<?php

class WebUser extends CWebUser {

    public function getProfile() {
        if (($profile = $this->getState('__profile')) !== null)
            return $profile;
        else
            return array();
    }

    public function setProfile($value) {
        $this->setState('__profile', (object)((array)$value));
    }
    
    public function getAvatar($id = 0) {
        $id = $id ? $id : $this->getId();
        return md5(Yii::app()->params['salt'] . $id) . '.png';
    }
    
    public function getAvatar_url($id = 0) {
        $avatar = $this->getAvatar($id);
        if(file_exists(Yii::getPathOfAlias('webroot.' . Yii::app()->params['avatar_dir']) . DIRECTORY_SEPARATOR . $avatar)) {
            return Yii::app()->baseUrl . '/' . Yii::app()->params['avatar_dir'] . '/' . $avatar;
        }
        return Yii::app()->baseUrl . '/' . Yii::app()->params['avatar_dir'] . '/' . 'default.png';
    }
    
    public function getIs_admin() {
        return (bool)$this->getState('is_admin');
    }
    
    public function checkAccess($operation,$params=array(),$allowCaching=true) {
        $method = 'check'.ucfirst($operation);
        if(method_exists($this, $method)) {
            return $this->$method($params);
        }
        return false;
    }
    
    public function checkOwnMeme($params) {
        return $params['meme']->user_fk == $this->getId();
    }
    
    public function checkOwnId($user_id) {
        return $user_id == $this->getId();
    }
}